package cn.bjc.system.controller;

import cn.bjc.basic.util.JsonResult;
import cn.bjc.basic.util.PageList;
import cn.bjc.system.domain.Systemdictionarydetail;
import cn.bjc.system.query.SystemdictionarydetailQuery;
import cn.bjc.system.service.ISystemdictionarydetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//数据字典-明细-接口
@RestController
@RequestMapping("/detail")
public class SystemdictionarydetailController {
    
    @Autowired
    private ISystemdictionarydetailService detailService;

    
    //查询所有明细
    @GetMapping
    public List<Systemdictionarydetail> findAll(){
        return detailService.loadAll();
    }
    
    
    //通过id查询明细
    @GetMapping("/{id}")
    public Systemdictionarydetail findOne(@PathVariable("id") Long id){
        return detailService.loadById(id);
    }
    
    //添加和修改
    @PutMapping()
    public JsonResult updateOrAdd(@RequestBody Systemdictionarydetail systemdictionarydetail){
        try {
            if(systemdictionarydetail.getId()==null){
                detailService.save(systemdictionarydetail);
            }else {
                detailService.update(systemdictionarydetail);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败,稍后重试");
        }
    }
    
    //通过id删除
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            detailService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败,稍后重试");
        }
    }
    
    //分页查询
    @PostMapping
    public PageList<Systemdictionarydetail> queryPage(@RequestBody SystemdictionarydetailQuery query){
        try {
            return detailService.pageQuery(query);
        } catch (Exception e){
            e.printStackTrace();
            return new PageList<Systemdictionarydetail>();
        }
    }
    
    //批量删除
    @PatchMapping()
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            detailService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }
    
    
}
